/*******************************************************************************

This code file produces Table A3, "Summary Statistics on 421-a Eligible Buildings, 2003--2014."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
* Load Stata-TeX

	do "$code/modules/stata-tex.do"
	cd "$tables/estimates"
	
*** Load data

	use "$data/clean/cleaned_data.dta", clear
		
********************************************************************************
* Table-specific data cleaning
********************************************************************************
	
	gen medhhinc = exp(lmedhhinc)
	gen meangrossrent = 12*exp(lmeangrossrent)
	gen sh_res = resarea / bldgarea
	
	foreach v of varlist sh_lths sh_hsdeg sh_somecoll sh_coll sh_postgrad sh_drive sh_bus sh_subway sh_walk sh_poor sh_res sh_renter sh_hispanic sh_white sh_black sh_asian sh_vacant condo histdist {
	
		replace `v' = `v'*100
	
	}
	
********************************************************************************
* Complete table analysis
********************************************************************************

	local varlist = "sh_lths sh_hsdeg sh_somecoll sh_coll sh_postgrad sh_drive sh_bus sh_subway sh_walk sh_poor sh_res sh_renter sh_hispanic sh_white sh_black sh_asian sh_vacant medhhinc medage bldgfront bldgdepth bldgarea meangrossrent sh_res condo histdist residfar"

	tabstat `varlist', by(inclusionary_onsite) stats(mean sd) col(stat) save nototal
		
	mat stats1 = r(Stat1)
	mat stats2 = r(Stat2)
	
	* sh_lths
	local v1 = stats1[1,1]
	local v2 = stats2[1,1]
	local v3 = stats1[2,1]
	local v4 = stats2[2,1]
	insert_into_file using tableA3.csv, key(sh_lths0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_lths1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_lths0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_lths1_sd) value(`v4') format(%12.1f)
	
	* sh_hsdeg
	local v1 = stats1[1,2]
	local v2 = stats2[1,2]
	local v3 = stats1[2,2]
	local v4 = stats2[2,2]
	insert_into_file using tableA3.csv, key(sh_hsdeg0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_hsdeg1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_hsdeg0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_hsdeg1_sd) value(`v4') format(%12.1f)
	
	* sh_somecoll
	local v1 = stats1[1,3]
	local v2 = stats2[1,3]
	local v3 = stats1[2,3]
	local v4 = stats2[2,3]
	insert_into_file using tableA3.csv, key(sh_somecoll0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_somecoll1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_somecoll0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_somecoll1_sd) value(`v4') format(%12.1f)
	
	* sh_coll
	local v1 = stats1[1,4]
	local v2 = stats2[1,4]
	local v3 = stats1[2,4]
	local v4 = stats2[2,4]
	insert_into_file using tableA3.csv, key(sh_coll0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_coll1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_coll0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_coll1_sd) value(`v4') format(%12.1f)
	
	* sh_postgrad
	local v1 = stats1[1,5]
	local v2 = stats2[1,5]
	local v3 = stats1[2,5]
	local v4 = stats2[2,5]
	insert_into_file using tableA3.csv, key(sh_postgrad0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_postgrad1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_postgrad0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_postgrad1_sd) value(`v4') format(%12.1f)
	
	* sh_drive
	local v1 = stats1[1,6]
	local v2 = stats2[1,6]
	local v3 = stats1[2,6]
	local v4 = stats2[2,6]
	insert_into_file using tableA3.csv, key(sh_drive0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_drive1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_drive0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_drive1_sd) value(`v4') format(%12.1f)
	
	* sh_bus
	local v1 = stats1[1,7]
	local v2 = stats2[1,7]
	local v3 = stats1[2,7]
	local v4 = stats2[2,7]
	insert_into_file using tableA3.csv, key(sh_bus0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_bus1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_bus0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_bus1_sd) value(`v4') format(%12.1f)
	
	* sh_subway
	local v1 = stats1[1,8]
	local v2 = stats2[1,8]
	local v3 = stats1[2,8]
	local v4 = stats2[2,8]
	insert_into_file using tableA3.csv, key(sh_subway0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_subway1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_subway0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_subway1_sd) value(`v4') format(%12.1f)
	
	* sh_walk
	local v1 = stats1[1,9]
	local v2 = stats2[1,9]
	local v3 = stats1[2,9]
	local v4 = stats2[2,9]
	insert_into_file using tableA3.csv, key(sh_walk0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_walk1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_walk0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_walk1_sd) value(`v4') format(%12.1f)
	
	* sh_poor
	local v1 = stats1[1,10]
	local v2 = stats2[1,10]
	local v3 = stats1[2,10]
	local v4 = stats2[2,10]
	insert_into_file using tableA3.csv, key(sh_poor0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_poor1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_poor0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_poor1_sd) value(`v4') format(%12.1f)
	
	* sh_res
	local v1 = stats1[1,11]
	local v2 = stats2[1,11]
	local v3 = stats1[2,11]
	local v4 = stats2[2,11]
	insert_into_file using tableA3.csv, key(sh_res0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_res1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_res0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_res1_sd) value(`v4') format(%12.1f)
	
	* sh_renter
	local v1 = stats1[1,12]
	local v2 = stats2[1,12]
	local v3 = stats1[2,12]
	local v4 = stats2[2,12]
	insert_into_file using tableA3.csv, key(sh_renter0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_renter1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_renter0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_renter1_sd) value(`v4') format(%12.1f)
	
	* sh_hispanic
	local v1 = stats1[1,13]
	local v2 = stats2[1,13]
	local v3 = stats1[2,13]
	local v4 = stats2[2,13]
	insert_into_file using tableA3.csv, key(sh_hispanic0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_hispanic1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_hispanic0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_hispanic1_sd) value(`v4') format(%12.1f)
	
	* sh_white
	local v1 = stats1[1,14]
	local v2 = stats2[1,14]
	local v3 = stats1[2,14]
	local v4 = stats2[2,14]
	insert_into_file using tableA3.csv, key(sh_white0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_white1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_white0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_white1_sd) value(`v4') format(%12.1f)
	
	* sh_black
	local v1 = stats1[1,15]
	local v2 = stats2[1,15]
	local v3 = stats1[2,15]
	local v4 = stats2[2,15]
	insert_into_file using tableA3.csv, key(sh_black0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_black1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_black0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_black1_sd) value(`v4') format(%12.1f)
	
	* sh_asian
	local v1 = stats1[1,16]
	local v2 = stats2[1,16]
	local v3 = stats1[2,16]
	local v4 = stats2[2,16]
	insert_into_file using tableA3.csv, key(sh_asian0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_asian1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_asian0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_asian1_sd) value(`v4') format(%12.1f)
	
	* sh_vacant
	local v1 = stats1[1,17]
	local v2 = stats2[1,17]
	local v3 = stats1[2,17]
	local v4 = stats2[2,17]
	insert_into_file using tableA3.csv, key(sh_vacant0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_vacant1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_vacant0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_vacant1_sd) value(`v4') format(%12.1f)
	
	* medhhinc
	local v1 = stats1[1,18]
	local v2 = stats2[1,18]
	local v3 = stats1[2,18]
	local v4 = stats2[2,18]
	insert_into_file using tableA3.csv, key(medhhinc0_mean) value(`v1') format(%12.0fc)
	insert_into_file using tableA3.csv, key(medhhinc1_mean) value(`v2') format(%12.0fc)
	insert_into_file using tableA3.csv, key(medhhinc0_sd) value(`v3') format(%12.0fc)
	insert_into_file using tableA3.csv, key(medhhinc1_sd) value(`v4') format(%12.0fc)
	
	* medage
	local v1 = stats1[1,19]
	local v2 = stats2[1,19]
	local v3 = stats1[2,19]
	local v4 = stats2[2,19]
	insert_into_file using tableA3.csv, key(medage0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(medage1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(medage0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(medage1_sd) value(`v4') format(%12.1f)
	
	* bldgfront
	local v1 = stats1[1,20]
	local v2 = stats2[1,20]
	local v3 = stats1[2,20]
	local v4 = stats2[2,20]
	insert_into_file using tableA3.csv, key(bldgfront0_mean) value(`v1') format(%12.0fc)
	insert_into_file using tableA3.csv, key(bldgfront1_mean) value(`v2') format(%12.0fc)
	insert_into_file using tableA3.csv, key(bldgfront0_sd) value(`v3') format(%12.0fc)
	insert_into_file using tableA3.csv, key(bldgfront1_sd) value(`v4') format(%12.0fc)
	
	* bldgdepth
	local v1 = stats1[1,21]
	local v2 = stats2[1,21]
	local v3 = stats1[2,21]
	local v4 = stats2[2,21]
	insert_into_file using tableA3.csv, key(bldgdepth0_mean) value(`v1') format(%12.0fc)
	insert_into_file using tableA3.csv, key(bldgdepth1_mean) value(`v2') format(%12.0fc)
	insert_into_file using tableA3.csv, key(bldgdepth0_sd) value(`v3') format(%12.0fc)
	insert_into_file using tableA3.csv, key(bldgdepth1_sd) value(`v4') format(%12.0fc)
	
	* bldgarea
	local v1 = stats1[1,22]
	local v2 = stats2[1,22]
	local v3 = stats1[2,22]
	local v4 = stats2[2,22]
	insert_into_file using tableA3.csv, key(bldgarea0_mean) value(`v1') format(%12.0fc)
	insert_into_file using tableA3.csv, key(bldgarea1_mean) value(`v2') format(%12.0fc)
	insert_into_file using tableA3.csv, key(bldgarea0_sd) value(`v3') format(%12.0fc)
	insert_into_file using tableA3.csv, key(bldgarea1_sd) value(`v4') format(%12.0fc)
	
	* meangrossrent
	local v1 = stats1[1,23]
	local v2 = stats2[1,23]
	local v3 = stats1[2,23]
	local v4 = stats2[2,23]
	insert_into_file using tableA3.csv, key(meangrossrent0_mean) value(`v1') format(%12.0fc)
	insert_into_file using tableA3.csv, key(meangrossrent1_mean) value(`v2') format(%12.0fc)
	insert_into_file using tableA3.csv, key(meangrossrent0_sd) value(`v3') format(%12.0fc)
	insert_into_file using tableA3.csv, key(meangrossrent1_sd) value(`v4') format(%12.0fc)
	
	* sh_res
	local v1 = stats1[1,24]
	local v2 = stats2[1,24]
	local v3 = stats1[2,24]
	local v4 = stats2[2,24]
	insert_into_file using tableA3.csv, key(sh_res0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_res1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_res0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(sh_res1_sd) value(`v4') format(%12.1f)
	
	* condo
	local v1 = stats1[1,25]
	local v2 = stats2[1,25]
	local v3 = stats1[2,25]
	local v4 = stats2[2,25]
	insert_into_file using tableA3.csv, key(condo0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(condo1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(condo0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(condo1_sd) value(`v4') format(%12.1f)
	
	* histdist
	local v1 = stats1[1,26]
	local v2 = stats2[1,26]
	local v3 = stats1[2,26]
	local v4 = stats2[2,26]
	insert_into_file using tableA3.csv, key(histdist0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(histdist1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(histdist0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(histdist1_sd) value(`v4') format(%12.1f)
	
	* residfar
	local v1 = stats1[1,27]
	local v2 = stats2[1,27]
	local v3 = stats1[2,27]
	local v4 = stats2[2,27]
	insert_into_file using tableA3.csv, key(residfar0_mean) value(`v1') format(%12.1f)
	insert_into_file using tableA3.csv, key(residfar1_mean) value(`v2') format(%12.1f)
	insert_into_file using tableA3.csv, key(residfar0_sd) value(`v3') format(%12.1f)
	insert_into_file using tableA3.csv, key(residfar1_sd) value(`v4') format(%12.1f)
	
********************************************************************************
* Create table
********************************************************************************
	
	cat tableA3.csv

	cap erase "$tables/output/tableA3.tex"

	cd "$code/modules"
	capture table_from_tpl, t("$tables/templates/tableA3.tex") ///
							r("$tables/estimates/tableA3.csv") ///
							o("$tables/output/tableA3.tex") 
			
	capture table_from_tpl, t("$tables/templates/tableA3.tex") ///
							r("$tables/estimates/tableA3.csv") ///
							o("$tables_overleaf/tableA3.tex") 
							
	exit
